package com.project.xsf.service.impl;

import com.project.junM.entity.TMember;
import com.project.junM.mapper.TBodyCkMapper;
import com.project.xsf.entity.TAssess;
import com.project.xsf.entity.TAssessAnswer;
import com.project.xsf.entity.TAssessQuestion;
import com.project.xsf.mapper.TAssessAnswerMapper;
import com.project.xsf.mapper.TAssessMapper;
import com.project.xsf.mapper.TAssessQuestionOptionMapper;
import com.project.xsf.service.TAssessAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class TAssessAnswerImpl implements TAssessAnswerService {
    @Autowired
    private TAssessAnswerMapper tAssessAnswerMapper;
    @Autowired
    private TAssessMapper tAssessMapper;
    @Autowired
    private TAssessQuestionOptionMapper tAssessQuestionOptionMapper;

    @Autowired
    private TBodyCkMapper tBodyCkMapper;
    @Override
    public int addAnswer(List<TAssessQuestion> tAssessQuestions, HttpSession session) {
        TMember user = (TMember) session.getAttribute("user");
        Integer markTotal=0;
        for(int i=0;i<tAssessQuestions.size();i++ ){
            TAssessQuestion tAssessQuestion = tAssessQuestions.get(i);
            TAssessAnswer tAssessAnswer = new TAssessAnswer();
            //设置问题id
            tAssessAnswer.setQuestionId(tAssessQuestion.getId());
            //设置答案id
            if(tAssessQuestion.getIsRadio().equals("0")){
                tAssessAnswer.setAnswerId(tAssessQuestion.getSelectOption()+"");
//                System.out.println(tAssessQuestionOptionMapper.selectByPrimaryKey(tAssessQuestion.getSelectOption()).getMark());
                markTotal+=tAssessQuestionOptionMapper.selectByPrimaryKey(tAssessQuestion.getSelectOption()).getMark();
            }else{
                String options="";
                for(int j=0;j<tAssessQuestion.getCheckOptions().size();j++){
                    options+=tAssessQuestion.getCheckOptions().get(j);
                    Double mark = Double.parseDouble(tAssessQuestion.getCheckOptions().get(j).toString());
//                    System.out.println(tAssessQuestionOptionMapper.selectByPrimaryKey(mark.intValue()).getMark());
                    markTotal+=tAssessQuestionOptionMapper.selectByPrimaryKey(mark.intValue()).getMark();
                    if(j<tAssessQuestion.getCheckOptions().size()-1){
                        options+=",";
                    }
                }
                tAssessAnswer.setAnswerId(options);
            }
            //设置当前登录人id
            tAssessAnswer.setUserId(user.getId());
            System.out.println(tAssessAnswer);
            tAssessAnswerMapper.insertSelective(tAssessAnswer);
        }
        /*TAssess tAssess = new TAssess();
        tAssess.setMemberId(user.getId());
        //判断用户是否体检上传
        Integer count = tBodyCkMapper.queryCountByMemberId(user.getId());
        if(count>0){
            tAssess.setState(1);
        }else{
            tAssess.setState(0);
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
        tAssess.setTime(simpleDateFormat.format(new Date()));
        int rows = tAssessMapper.insertSelective(tAssess);*/
//        System.out.println(tAssess);

        return 0;
    }

  /*  @Override
    public Boolean queryAnswerCountById(HttpSession session, Integer bankId) {
        return null;
    }*/

    @Override
    public Boolean queryAnswerCountById(HttpSession session,Integer bankId) {
        TMember user = (TMember) session.getAttribute("user");
        System.out.println(user);
        System.out.println(bankId);
        Integer count = tAssessAnswerMapper.queryAnswerCountById(user.getId(),bankId);
        if(count>0){
            return true;
        }else {
            return false;
        }
    }
}
